﻿#region License
//------------------------------------------------------------
// Copyright (c) Denis Vuyka.  All rights reserved.
// This source code is subject to terms and conditions of the 
// Microsoft Reciprocal License (Ms-RL) License for Carcass. 
// A copy of the license can be found in the License.txt file 
// at the root of this distribution. 
// By using this source code in any fashion, you are agreeing 
// to be bound by the terms of the 
// Microsoft Reciprocal License (Ms-RL) License
// You must not remove this notice from this software.
//------------------------------------------------------------
#endregion

using System.ComponentModel.Composition;
using System.IO;
using Carcass.Contents;

namespace DemoShell.Modules.TextDocument
{
  [Export(Carcass.GlobalContracts.DocumentReader)]
  [ExportMetadata("DocumentType", DocumentTypes.TextFile)]
  [ExportMetadata("FilterTitle", "Text Files")]
  [ExportMetadata("Extension", ".txt", IsMultiple = true)]
  [ExportMetadata("Extension", ".xml", IsMultiple = true)]  
  public sealed class TextDocumentReader : IDocumentReader
  {
    [Import]
    private IDocumentService DocumentService { get; set; }

    public Export<IDocument, IDocumentMetadata> Read(Stream stream)
    {
      var document = DocumentService.CreateDocument(DocumentTypes.TextFile);

      IDocument instance = document.GetExportedObject();
      if (instance.CanLoad)
      {
        stream.Seek(0, SeekOrigin.Begin);
        instance.Load(stream);
      }
      return document;
    }
  }  
}
